
//光标的位置  0 选择产品类型 1 保单信息 -1查看保单信息 -7 减份数 7 加份数  2确定投保 -2 确认投保页面 3选择支付方式页面 4 扫码页面 5 结果页面 8 订单详情页面返回首页  -8 订单详情页面 详情。

var focus = 0;
var goodNum = 1 ;
var insuranceObj = null;
//支付方式 1 支付宝 2 微信
var pay =1;

//存放请求 一旦返回/请求成功时  清空所有请求
var  ajaxArray = new Array();

var isClosed =false;

//定时器 是否支付
var timerPay = null;
//是否超时
var timerOver = null;

window.onload = function () {
    var id = GetQueryString("id");
    if(id!=null){
        getInsuranceObj(id);
    }
};

function showInsuranceObj() {
    $("name").innerHTML=insuranceObj.name;
    $("p_total").innerHTML=insuranceObj.money;
    $("priceList").innerHTML='<div class="price"> ' +
        '<span class="in-title">'+insuranceObj.name+'</span> ' +
        '<span class="in-money" id="in-1">￥'+insuranceObj.money+'.00</span> </div>';
    initInsurance()
    
}

function initInsurance() {
    showInsuranceInfo(goodNum);
    addBorderPrice(goodNum);
}
var tc_name = 'jn-';
var tc_index =1;
var tc_total =21;
var tc_page = 1;
var tc_files = 'bxtk';
function tanChuang() {
    var name = $("tp_title");
   if(focus==1){
       name.innerHTML='保单详情';
        showHiddenBD();
   }else if(focus==3 && pay==6){
       name.innerHTML='服务条款';
       showHiddenBD();
   }else if(focus==3 && ( pay==3 || pay==4 || pay==5)) {
       if(pay==3){
           $("info-duty").innerHTML=insuranceObj.tiaokuan;
       }else if(pay==4){
           $("info-duty").innerHTML=insuranceObj.goumaixz;
       }else if(pay==5){
           $("info-duty").innerHTML=insuranceObj.toubaorensm;
       }
   }
}

function showHiddenBD() {
    var duty = $("duty");
    duty.innerHTML='';
    if(focus==1){
        duty.innerHTML+='<div>使用期限：'+insuranceObj.qixian.number+insuranceObj.qixian.danwei+'</div><div class="contentTitle">产品优势</div>';
        for(var i = 0 ;i<insuranceObj.youshi.length;i++){
            duty.innerHTML+='<div>'+insuranceObj.youshi[i].content+'</div>';
        }
        duty.innerHTML+='<div class="contentTitle">常见问题</div>';
        for(var w = 0 ;w<insuranceObj.wenda.length;w++){
            duty.innerHTML+='<div>问：'+insuranceObj.wenda[w].content+'</div>' +
                '<div>答：'+insuranceObj.wenda[w].da[0].content+'</div>';
        }
        duty.innerHTML+='<div class="contentTitle">理赔流程</div>';
        for(var p = 0 ;p<insuranceObj.lipei.length;p++){
            duty.innerHTML+='<div>'+(p+1)+'：'+insuranceObj.lipei[p].content+'</div>' ;
        }
    }else if(focus==3 && pay==6){
        var time = getDate();
        var end = returnEndTime(time,insuranceObj.qixian.danwei,insuranceObj.qixian.number);
        if(insuranceObj.template.value=='accident'){
            duty.innerHTML='<div>被保人年龄：0~80（岁）</div>' +
                '<div>旅行周期：'+insuranceObj.qixian.number+insuranceObj.qixian.danwei+'</div> ' +
                '<div>投保人姓名：'+userObj.custname+'</div> ' +
                '<div>投保人身份证：'+userObj.idcardid+'</div>'+
                '<div>投保人手机号：'+userObj.phone+'</div>'+
                '<div>与投保人关系：本人</div>'+
                '<div>赔付标准：<span id="zr"></span></div>';
        }else{
            duty.innerHTML='<div>房屋信息：'+userObj.addr+'</div>' +
                '<div>起保日期：'+time+'</div> ' +
                '<div>起保止期：'+end+'</div> ' +
                '<div>投保人：'+userObj.custname+'</div> ' +
                '<div>与投保人关系：本人</div>'+
                '<div>赔付标准：<span id="zr"></span></div>';
        }
        showZR();
    }
}

function returnEndTime(time,danwei,num) {
    console.log(time+"--- "+danwei);
    var end= '';
    if(danwei=='天'){
        end= addDayGetTime(time,num);
    }else if(danwei=='年'){
        end= addYearsGetTime(time,num);
    }else if(danwei=='月'){
        end= addMonthGetTime(time,num);
    }

    return end;
}

function showTC() {
    $("tc-page").innerHTML='';
    var show_page = 15;
    var totalPage = Math.ceil(tc_total/15);
    if(tc_page == totalPage){
        show_page = tc_total;
    }else {
        show_page = tc_page*15;
    }
    var startIndex = (tc_page-1)*15;
    var num =1;
    for(var i = startIndex ;i<show_page;i++){
            createPage(num,i+1);
            num++;
    }
    console.log("[img url ]");
    console.log('img/'+tc_files+'/'+tc_name+((tc_page-1)*15+tc_index)+'.jpg');
    $("tc-info").src='img/'+tc_files+'/'+tc_name+((tc_page-1)*15+tc_index)+'.jpg';
    var obj = $("page-"+tc_index);
    if(obj!=null){
        obj.className+=' bg';
    }
}

function createPage(num,n) {
    var tc = $("tc-page");
    var className ='';
    tc.innerHTML+=' <div class="tc-common" id="page-'+num+'">'+n+'页</div>';
}
function showInsuranceInfo(num) {
    $("date").innerHTML=insuranceObj.qixian.number+insuranceObj.qixian.danwei;
    //优势
    var ys = $("ys");
    ys.innerHTML='';
    for(var i = 0 ;i<insuranceObj.youshi.length;i++){
        ys.innerHTML+='<div>'+insuranceObj.youshi[i].content+'</div>';
    }
    var wt = $("wt");
    wt.innerHTML='';
    for(var w = 0 ;w<insuranceObj.wenda.length;w++){
        wt.innerHTML+='<div>问：'+insuranceObj.wenda[w].content+'</div>' +
            '<div>答：'+insuranceObj.wenda[w].da[0].content+'</div>';
    }
    var lp = $("lp");
    lp.innerHTML='';
    for(var p = 0 ;p<insuranceObj.lipei.length;p++){
        wt.innerHTML+='<div>'+(p+1)+'：'+insuranceObj.lipei[p].content+'</div>' ;
    }
}



function addBorderPrice(){
    var obj = $("priceListActive");
    obj.style.display='block';
    $("in-1").className+= ' in-active';
}

function fun_right(){
    if(focus==0){

    }else if(focus==3){
        if(pay==1){
            /*pay=2;
            addBorderPay(pay);*/
        }else if(pay>2 && pay<5){
            removeClass('active-border',$("i-"+pay));
            pay++;
            addBorderPay(pay);
        }
    }else if(focus==-3){
        var len = $("tc-page").children.length;
        if(tc_index<len){
            var objTc = $("page-"+tc_index);
            if(objTc!=null){
                removeClass("bg",objTc);
            }
            tc_index++;
            var after = $("page-"+tc_index);
            after.className+=' bg';
            $("tc-info").src='img/'+tc_files+'/'+tc_name+((tc_page-1)*15+tc_index)+'.jpg';
        }else if(tc_index == len &&  tc_page < Math.ceil(tc_total/15)){
                tc_page++;
                tc_index=1;
                showTC();
        }

    }else if(focus==5){
        if(result.id=='suBack'){
            result =$("suOrder");
            $("suActive").className='su-2';
        }
    }else if(focus==1){
        removeClass("clauseA",$("clauseA-"+focus));
        focus=7;
        $("clauseA-"+focus).className+=' clauseA';
    }else if(focus==7){
        removeClass("clauseA",$("clauseA-"+focus));
        focus=9;
        $("clauseA-"+focus).className+=' clauseA';
    }
}

function fun_left() {
    if(focus==0){

    }else if(focus==3){
        if(pay==2 || pay==1){
           /* pay=1;
            addBorderPay(pay);*/
        }else if(pay>3 && pay <6){
            removeClass('active-border',$("i-"+pay));
            pay--;
            addBorderPay(pay);
        }
    }else if(focus==-3){
        if(tc_index>1){
            var objTc = $("page-"+tc_index);
            if(objTc!=null){
                removeClass("bg",objTc);
            }
            tc_index--;
            var after = $("page-"+tc_index);
            after.className+=' bg';
            $("tc-info").src='img/'+tc_files+'/'+tc_name+((tc_page-1)*15+tc_index)+'.jpg';
        }else if(tc_index ==1 &&  tc_page>1){
            tc_page--;
            tc_index=15;
            showTC();
        }

    }else if(focus==5){
        if(result.id=='suOrder'){
            result =$("suBack");
            $("suActive").className='su-1';
        }
    }else if(focus==9){
        removeClass("clauseA",$("clauseA-"+focus));
        focus=7;
        $("clauseA-"+focus).className+=' clauseA';
    }else if(focus==7){
        removeClass("clauseA",$("clauseA-"+focus));
        focus=1;
        $("clauseA-"+focus).className+=' clauseA';
    }
}

var userObj = null;

var product_num =1;
function fun_enter() {
    if(focus==0){
        showInsuranceInfo(goodNum);
    }else if(focus==1){
        $("termsOfPolicy").style.display='block';
        tanChuang();
        focus=-1;
    }else if(focus==-1){
        $("termsOfPolicy").style.display='none';
        focus=1;
    }else if(focus==2){
        //到确认投保页面
        userObj = getUserInfo();
        if(userObj!=null){
            var year  = returnYear(userObj.idcardid);
            if(year>=80 || year==null){
                showMessage("用户年龄不符合要求！如有疑问，请致电96655！");
                return;
            }
            var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;//&& !mobile.test(userObj.phone)
            if (!myreg.test(userObj.phone)) {
                showMessage("预留手机号异常！如有疑问，请致电96655！");
                return;
            }
            try{
                showAffirmInfo();
            }catch (error){
                showMessage("用户信息异常，请退出重试！如有疑问，请致电96655！");
            }
        }
    }else if(focus==-2){
        //传参money
        $("affirm").style.display='none';
        choosePay();
    }else if(focus==3){//选择支付方式页面
        if(pay==4 || pay==3 || pay==5){//购买须知
            tanChuang();
            $("pay-code").style.display='none';
            $("tc_duty").style.display='block';
            focus =-3;
        }else if(pay==1 || pay==2){
            $("pay-code").style.display='none';
            code(2,insuranceObj.money,product_num);
        }else if(pay==6){//更多详情
            tanChuang();
            $("pay-code").style.display='none';
            $("termsOfPolicy").style.display='block';
            focus =-6;
        }
    }else if(focus==-3){
        focus =3;
        $("pay-code").style.display='block';
        $("tc_duty").style.display='none';
    }else if(focus==5){
        if(result.id=='suBack'){//返回首页
            /*focus=2;
            $("pay").style.display='none';*/
            goBack("index");
        }else if(result.id=='suOrder'){//查看订单
            showOrder();
        }
    }else if(focus==-6){
        focus=3;
        $("tc_duty").style.display='none';
        $("termsOfPolicy").style.display='none';
        $("pay-code").style.display='block';
    }else if(focus==8){//返回首页
       /* focus=2;
        $("pay").style.display='none';
       // window.location = 'index.html';*/
        goBack("index");
    }else if(focus==7){
        product_num =1;
        $("p_total").innerHTML=insuranceObj.money*product_num;
        $("p_num").innerHTML=product_num+"份";
    }else if(focus==9){
        product_num =2;
        $("p_total").innerHTML=insuranceObj.money*product_num;
        $("p_num").innerHTML=product_num+"份";
    }
}

function returnYear(year) {
    var date = new Date();

    var newYear = date.getFullYear();

    var str = year.substr(6,4);

    if(str.split("").length==4){
        return newYear-str;
    }else {
        return null;
    }


}

function showOrder() {
    focus=8;
    var parNode = $("pay");
    parNode.style.background='url(img/bg2.png)';
    var time = getDate();
    var end = returnEndTime(time,insuranceObj.qixian.danwei,insuranceObj.qixian.number);
    parNode.innerHTML = '<div class="o_active" id="o_active"></div>' +
        ' <div class="orderInfo">订单详情</div> ' +
        '<div class="o_back">返回首页</div>'+
        '<div class="o_content" id="o_content">'+
        '<div class="o_show" id="order">'+
        '<div class="o_tag">订单信息</div>'+
        '<div><span class="o_name">订单号</span><span class="o_num">'+insuranceObj.id+'</span></div>'+
        '<div><span class="o_name">下单时间</span><span class="o_num">'+getDate()+'  '+getTimeC()+'</span></div>'+
        '<div><span class="o_name">订单金额</span><span class="o_num">'+insuranceObj.money*product_num+'元</span></div>'+
        '<div><span class="o_name">订单状态</span><span class="o_num">已付款</span></div>'+
        '<div class="o_tag">投保人信息</div>'+
        '<div><span class="o_name">姓名</span><span class="o_num">'+userObj.custname+'</span></div>'+
        '<div><span class="o_name">身份证号</span><span class="o_num">'+userObj.idcardid+'</span></div>'+
        '<div><span class="o_name">手机号</span><span class="o_num">'+userObj.phone+'</span></div>'+
        '<div class="o_tag">产品信息</div>'+
        '<div><span class="o_name">产品名称</span><span class="o_num">'+insuranceObj.name+'</span></div>'+
        '<div><span class="o_name">保险期限</span><span class="o_num">'+time+'-'+end+'</span></div>'+
        '<div><span class="o_name">保险金额</span><span class="o_num">'+insuranceObj.money+'元</span></div>'+
        '</div></div>' ;

    o_text = $("order");
    $("o_content").style.background='url()';
}

function showAffirmInfo() {
    var time = getDate();
    var end = returnEndTime(time,insuranceObj.qixian.danwei,insuranceObj.qixian.number);
    var affirmNode  = $("af-info");
    if(insuranceObj.template.value=='accident'){
        affirmNode.innerHTML='<div class="af-div"> <div class="af-name">被保人年龄</div> <span>：0~80（岁）</span> </div>' +
            '<div class="af-div"> <div class="af-name">旅行周期</div> <span>：'+insuranceObj.qixian.number+insuranceObj.qixian.danwei+'</span> </div> ' +
            '<div class="af-div"> <div class="af-name">起保止期</div> <span>：'+end+'</span> </div> ' +
            '<div class="af-div"> <div class="af-name">投保人姓名</div> <span>：'+userObj.custname+'</span> </div> ' +
            '<div class="af-div"> <div class="af-name">投保人身份证</div> <span>：'+userObj.idcardid+'</span> </div>'+
            '<div class="af-div"> <div class="af-name">投保人手机号</div> <span>：'+userObj.phone+'</span> </div>'+
            '<div class="af-div"> <div class="af-name">与投保人关系</div> <span>：本人</span> </div>';
    }else{
        affirmNode.innerHTML='<div class="af-div"> <div class="af-name">房屋信息</div> <span>：'+userObj.addr+'</span> </div>' +
            '<div class="af-div"> <div class="af-name">起保日期</div> <span>：'+time+'</span> </div> ' +
            '<div class="af-div"> <div class="af-name">起保止期</div> <span>：'+end+'</span> </div> ' +
            '<div class="af-div"> <div class="af-name">投保人姓名</div> <span>：'+userObj.custname+'</span> </div> ' +
            '<div class="af-div"> <div class="af-name">与投保人关系</div> <span>：本人</span> </div>';
    }
    focus=-2;
    $("affirm").style.display='block';
}

//-2 确认投保页面 3选择支付方式页面 4 扫码页面 5 结果页面
function fun_back() {
  if(focus==0 || focus==1 || focus==2){
      goBack();
  }else if(focus==-1){
      $("termsOfPolicy").style.display='none';
      focus=1;
  }else if(focus==-2){
      focus=2;
      $("affirm").style.display='none';
  }else if(focus==3){
      focus=-2;
      $("pay-code").style.display='none';
      $("affirm").style.display='block';
  }else if(focus==-3 || focus==-6){
      focus=3;
      $("tc_duty").style.display='none';
      $("termsOfPolicy").style.display='none';
      $("pay-code").style.display='block';
  }else if(focus==4){//扫码页面返回
      //清除定时器  中断请求
      $("pay").style.display='none';
      clearInterval(timerOver);
      abortAll();
      choosePay();
  }else if(focus==5 || focus==8){//扫码成功页面返回
      goBack("index");
      /*focus=2;
      $("pay").style.display='none';
      //window.location = 'list.html?id=8adb70fb60531aa901605323b1910004&name=财产&type=2'*/
  }
}
var tc_duty = $("info-duty");
function fun_down() {
    if(focus==0){
        focus=1;
        $("clauseA-"+focus).className+=' clauseA';
        //$("dtActive").style.display='block';
        $("sbActive").style.display='none';
    }else if(focus==1 || focus==7 || focus==9){
        removeClass("clauseA",$("clauseA-"+focus));
        focus=2;
        $("sbActive").style.display='block';
    }else if(focus==3){
        if(pay==3 || pay==4 || pay==5){
            removeClass('active-border',$("i-"+pay));
            pay=1;
            addBorderPay(pay);
        }else if(pay==6){
            pay=5;
            addBorderPay(pay);
        }
    }else if(focus==-1 || focus==-6){
        text.scrollTop = text.scrollTop + scroll_height;
    }else if(focus==-3){
        tc_duty.scrollTop = tc_duty.scrollTop + scroll_height;
    }else if(focus==8){
        $("o_active").style.display='none';
        focus=-8;
        console.log(text);
        $("o_content").style.background='url(img/s_bg.png)';
    }else if(focus==-8){
        o_text.scrollTop = o_text.scrollTop + scroll_height;
    }
    
}
var o_text = $("order");
var text = document.getElementById("duty");
var scroll_height =56;
function fun_up() {
    if(focus==1){
        focus=0;
        //$("dtActive").style.display='none';
    }else if(focus==2){
        focus=1;
        $("clauseA-1").className+=' clauseA';
        $("sbActive").style.display='none';
    }else if(focus==3){
        if(pay==1 || pay==2){
            pay=3;
            addBorderPay(pay);
        }else if(pay==3 || pay==4 || pay==5){
            removeClass('active-border',$("i-"+pay));
            pay=6;
            addBorderPay(pay);
        }
    }else if(focus==-1 || focus==-6){
        text.scrollTop = text.scrollTop - scroll_height;
    }else if(focus==-3){
        tc_duty.scrollTop = tc_duty.scrollTop - scroll_height;
    }else if(focus==8){

    }else if(focus==-8){
        if(o_text.scrollTop==0){
            $("o_active").style.display='block';
            focus=8;
            $("o_content").style.background='url()';
        }else {
            o_text.scrollTop = o_text.scrollTop - scroll_height;
        }
    }

}

function addBorderPay(num) {
    var obj = $("focus");
    if(num>0){
        if(num==1 || num==2){
            obj.className='pay-focus';
            obj.style.top= 554+'px';
            obj.style.left=(458 +386*(num-1))+ 'px';
            obj.style.display='block'
        }else if(num==3 || num==4 || num==5) {
            var i = $("i-"+num);
            if(i!=null){
                i.className+=' active-border';
            }
            obj.style.display='none'
        }else if(num==6) {
            obj.className='pay-btn';
            obj.style.top= 330+'px';
            obj.style.left= 984+'px';
            obj.style.display='block'
        }
    }
}

function goBack(index) {
    if(index=='index'){
        var index_url = GetQueryString("index_url");
        if(index_url!=null && index_url!='' && index_url!='null'){
            window.location.href =decodeURIComponent(index_url);
            return null;
        }
    }
    var back_url = GetQueryString("back_url");
    if(back_url!=null && back_url!='' && back_url!='null'){
        window.location.href =decodeURIComponent(back_url);
    }else {
        window.history.go(-1);
    }
}